
goog.provide('esn.NetworkSurvey');

goog.require('esn.BaseSurvey');
goog.require('esn.NetworkSurveyQuestions');
goog.require('esn.NetworkSurveySection');
goog.require('esn.constants');
goog.require('esn.dialog.NetworkSurveyDialog');
goog.require('esn.model.QuestionModel');
goog.require('esn.soy.networkSurvey');
goog.require('goog.dom');
goog.require('goog.dom.classes');
goog.require('goog.style');
goog.require('goog.ui.CustomButton');
goog.require('goog.ui.Dialog');


/**
 * @constructor
 */
esn.NetworkSurvey = function(appContext, opt_domHelper) {
  goog.base(this, appContext, esn.NetworkSurvey.SECTION_ID, opt_domHelper);

  this.appContext_ = appContext;

  this.surveyClient_ = this.appContext_.getService(
      esn.constants.ServiceId.SURVEY_CLIENT);

  this.sectionCount_ = 1;
};
goog.inherits(esn.NetworkSurvey, esn.BaseSurvey);

esn.NetworkSurvey.SECTION_ID = 'network';

esn.NetworkSurvey.EventType = {
  BACK: goog.events.getUniqueId('back'),
  CONTINUE: goog.events.getUniqueId('continue')
};

esn.NetworkSurvey.TitleBackgroundColors_ =
    ['#C9EDC1', '#C7CCF5', '#D4ABE9', '#FFCC99'];

esn.NetworkSurvey.prototype.friendSurveys_;

esn.NetworkSurvey.prototype.surveySection_;

esn.NetworkSurvey.prototype.continueButton_;

esn.NetworkSurvey.prototype.backButton_;

esn.NetworkSurvey.prototype.alertDialog_;

esn.NetworkSurvey.prototype.setFriendSurveys = function(friends) {
  this.friendSurveys_ = friends;
  // If we're already rendered then let's refresh the dom to display the update
  // friend network.
  if (this.getElement()) {
    this.createSectionDom_();
  }
};

esn.NetworkSurvey.prototype.enterDocument = function() {
  goog.base(this, 'enterDocument');

  this.getHandler().listen(
      this.continueButton_,
      goog.ui.Component.EventType.ACTION,
      this.handleContinueClick_);

  this.getHandler().listen(
      this.backButton_,
      goog.ui.Component.EventType.ACTION,
      this.handleBackClick_);
};

esn.NetworkSurvey.prototype.createDom = function() {
  goog.base(this, 'createDom');

  if (!this.friendSurveys_) {
    throw new Error(
        'Network survey cannot be rendered until the friend surveys are set.');
  }

  this.setElementInternal(soy.renderAsElement(esn.soy.networkSurvey.content));

  this.createSectionDom_();

  this.continueButton_ = new goog.ui.CustomButton(
      esn.constants.getContinueButtonCaption());
  this.continueButton_.render(this.getElementByClass('continueButton'));

  this.backButton_ = new goog.ui.CustomButton(
      esn.constants.getBackButtonCaption());
  this.backButton_.render(this.getElementByClass('backButton'));
};

esn.NetworkSurvey.prototype.createSectionDom_ = function() {
  var question = new esn.model.QuestionModel(
      esn.NetworkSurveyQuestions[this.sectionCount_ - 1],
      esn.NetworkSurvey.SECTION_ID);

  // Set section title.
  goog.dom.setTextContent(this.getElementByClass('surveyTitle'),
      question.getQuestion());

  // Set section instructions.
  var instructionsEl =
      goog.dom.htmlToDocumentFragment(question.getDescription());
  goog.dom.removeChildren(this.getElementByClass('surveyInstructions'));
  goog.dom.appendChild(this.getElementByClass('surveyInstructions'),
      instructionsEl);

  // Remove any previous rendered section.
  if (this.surveySection_) {
    this.removeChild(this.surveySection_, true);
    this.surveySection_.dispose();
  }

  // Render active network survey section.
  this.surveySection_ = new esn.NetworkSurveySection(this.appContext_, question,
      this.friendSurveys_);
  this.addChild(this.surveySection_);
  this.surveySection_.render(this.getElementByClass('sectionContainer'));

  this.setTitleBackgroundColor_();
};

esn.NetworkSurvey.prototype.setTitleBackgroundColor_ = function() {
  var titleEls = this.getElementsByClass('friendListTitle');
  var backgroundColor =
    esn.NetworkSurvey.TitleBackgroundColors_[this.sectionCount_ - 1]
  goog.array.forEach(titleEls, function(titleEl) {
    // Don't change the title background color of network box.
    if (!goog.dom.classes.has(titleEl, 'networkTitle')) {
      goog.style.setStyle(titleEl, 'background', backgroundColor);
    }
  });
};

esn.NetworkSurvey.prototype.handleContinueClick_ = function(e) {
  if (!this.surveySection_.areAllFriendsMoved_()) {
    this.displayAlertDialog_();
  } else if (this.sectionCount_ < esn.NetworkSurveyQuestions.length) {
    // Update progress bar.
    this.dispatchChangeEvent_();
    // Display next section of network survey.
    this.sectionCount_++;
    this.createSectionDom_();
  } else {
    // Update progress bar.
    this.dispatchChangeEvent_();
    // We're done, so let's dispatch the CONTINUE event.
    this.dispatchEvent(esn.NetworkSurvey.EventType.CONTINUE);
  }
};

esn.NetworkSurvey.prototype.displayAlertDialog_ = function() {
  if (!this.alertDialog_) {
    this.alertDialog_ = new esn.dialog.NetworkSurveyDialog();
  }
  this.alertDialog_.setVisible(true);
};

/**
 * Dispatches change event to update progress bar.
 */
esn.NetworkSurvey.prototype.dispatchChangeEvent_ = function() {
  this.dispatchEvent(new esn.BaseSurvey.ChangeEvent(
      esn.NetworkSurvey.SECTION_ID,
      esn.NetworkSurveyQuestions.length,
      this.sectionCount_));
};

esn.NetworkSurvey.prototype.handleBackClick_ = function(e) {
  if (this.sectionCount_ == 1) {
    this.dispatchEvent(esn.NetworkSurvey.EventType.BACK);
  } else {
    this.sectionCount_--;
    this.createSectionDom_();
  }
};
